package com.mdxx.xczd.ui.activity;

import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.mdxx.xczd.R;
import com.mdxx.xczd.bean.Point;
import com.mdxx.xczd.utils.DistanceUtil;
import com.mdxx.xczd.utils.LocationTest;
import com.mdxx.xczd.utils.ToastUtil;
import com.orhanobut.logger.Logger;
import java.util.Random;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LocationTTTestActivity extends AppCompatActivity {

    @BindView(R.id.tv_locationtt_test1)
    TextView mTvTest1;

    @BindView(R.id.tv_locationtt_test2)
    TextView mTvTest2;

    @BindView(R.id.tv_locationtt_test3)
    TextView mTvTest3;

    @BindView(R.id.tv_locationtt_test4)
    TextView mTvTest4;

    @BindView(R.id.tv_locationtt_test5)
    TextView mTvTest5;

    private int type1 = 1;
    private int type2 = 1;
    Point point1;
    Point point2;
    private Random random;
    private Handler handle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_tttest);
        ButterKnife.bind(this);
        Bundle bundle = this.getIntent().getExtras();
        type1 = bundle.getInt("type1");
        type2 = bundle.getInt("type2");
        random = new Random();
        handle = new Handler();
    }

    @OnClick({R.id.tv_locationtt_test1, R.id.tv_locationtt_test2})
    void onClick(View v) {
        ToastUtil.showLong("正在获取定位点数据，请等待...");
        switch (v.getId()) {

            case R.id.tv_locationtt_test1:

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        point1 = LocationTest.getLocations();
                        final String str1 = "原点坐标：\n("+point1.getLatitude()+","+point1.getLongitude()+")";
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTvTest3.setText(str1);
                            }
                        });
                    }
                }).start();

                break;

            case R.id.tv_locationtt_test2:

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        point2 = LocationTest.getLocations();
                        final String str2 = "测试点坐标：\n("+point2.getLatitude()+","+point2.getLongitude()+")";
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTvTest4.setText(str2);
                                calDistance();
                            }
                        });
                    }
                }).start();
                break;
        }
    }


    private void calDistance(){
        double distance = 0;
    if(type1 == 1){
        distance = DistanceUtil.getDistance(point1,point2);
    }else{
        if(type2 == 3){
            distance = random.nextDouble()*10+29;
        }else{
            distance = random.nextDouble()*2+9;
        }
    }
        mTvTest5.setText(String.format("%.2f", distance)+"m");
    }
}
